package cn.wolfcode.p2p.business.domain;

import cn.wolfcode.p2p.base.domain.BaseDomain;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.util.CalculatetUtil;
import cn.wolfcode.p2p.util.Constants;
import com.alibaba.fastjson.JSON;
import lombok.Getter;
import lombok.Setter;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

import static cn.wolfcode.p2p.util.Constants.*;

@Getter@Setter
public class BidRequest extends BaseDomain {
    private Integer version=0;

    private Integer bidRequestType=Constants.BIDREQUEST_TYPE_NORMAL;// 借款类型,默认信用贷

    private Integer bidRequestState=Constants.BIDREQUEST_STATE_APPLY;//借款状态,默认待审核

    private BigDecimal bidRequestAmount;//借款金额

    private BigDecimal currentRate;//借款利息

    private int monthes2Return; //分成几期  ,,

    private Integer bidCount=0;//投标次数

    private BigDecimal totalRewardAmount;//要还的总利息

    private BigDecimal currentSum=Constants.ZERO;//当前投标总额

    private String title;//借款标题

    private String description;//借款详情

    private String note;//风控意见

    private Date disableDate;//招标到期时间

    private LoginInfo createUser;//借款人

    private Integer disableDays;//投标天数

    private BigDecimal minBidAmount;//最小投标金额

    private Date applyTime;//申请时间

    private Date publishTime;//发布时间
    private Integer returnType=Constants.RETURN_TYPE_MONTH_INTEREST_PRINCIPAL;
    private List<Bid> bids = new ArrayList<>();//关联投标对象

    /**
     * 复写equals方法
     * @return
     */

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        BidRequest that = (BidRequest) o;
        return Objects.equals(id, that.id);
    }

    @Override
    public int hashCode() {

        return Objects.hash(id);
    }
    public BigDecimal getRemainAmount(){
        return this.getBidRequestAmount().subtract(this.getCurrentSum());
    }


    public String getBidRequestStateDisplay(){
        switch (this.bidRequestState){
            case BIDREQUEST_STATE_APPLY : return "申请中";
            case BIDREQUEST_STATE_PUBLISH_PENDING : return "待发布";
            case BIDREQUEST_STATE_BIDDING : return "招标中";
            case BIDREQUEST_STATE_UNDO : return "已撤销";
            case BIDREQUEST_STATE_BIDDING_OVERDUE : return "流标";
            case BIDREQUEST_STATE_APPROVE_PENDING_1 : return "满标1审";
            case BIDREQUEST_STATE_APPROVE_PENDING_2 : return "满标2审";
            case BIDREQUEST_STATE_REJECTED : return "满标审核被拒绝";
            case BIDREQUEST_STATE_PAYING_BACK : return "还款中";
            case BIDREQUEST_STATE_COMPLETE_PAY_BACK : return "已还清";
            case BIDREQUEST_STATE_PAY_BACK_OVERDUE : return "逾期";
            case BIDREQUEST_STATE_PUBLISH_REFUSE : return "发标审核拒绝状态";
        }
        return "状态异常";
    }
    //显示投标进度
    public int getPersent(){
        return this.getCurrentSum().divide(bidRequestAmount,Constants.SCALE_CAL, RoundingMode.HALF_UP)
                .multiply(CalculatetUtil.ONE_HUNDRED).intValue();
    }

    public String getJsonString(){
        HashMap<String , Object> map = new HashMap<>();
        map.put("id",id);
        map.put("username",createUser.getUsername());
        map.put("title",title);
        map.put("bidRequestAmount",bidRequestAmount);
        map.put("currentRate",currentRate);
        map.put("monthes2Return",monthes2Return);
        map.put("returnType",getReturnTypeDisplay());
        map.put("totalRewardAmount",totalRewardAmount);
        return JSON.toJSONString(map);
    }

    public String getReturnTypeDisplay(){
        return returnType==Constants.RETURN_TYPE_MONTH_INTEREST_PRINCIPAL?"按月分期":"按月到期";
    }


}